/**
 * 自定义指令
 */


//v-has 检查是否具备 角色、API,隐藏或展示组件
export const hasPermission = {
    install(Vue: any, options: any) {
        //自定义指令v-has：
        Vue.directive('has', {
            mounted(el: any, binding: any) {
                if (!checkPermission(binding.value)) {
                    el.parentNode.removeChild(el);
                }
            },
        });

        //权限检查方法
        function checkPermission(value: any) {
            let status = false;

            // //先检查角色
            //
            // //再检API
            //
            // let buttonArr = JSON.parse(localStorage.getItem("permissionButton") || "[]");
            //
            // //判断是否按钮有权限
            // if (buttonArr.includes(value)) {
            //     status = true;
            // }
            return status;

        }
    }
};

//v-resize
export const resize = {
    install(Vue: any, options: any) {
        Vue.directive('resize', {
            mounted(el, binding, vnode) {
                // 这里使用debounce防抖处理，防抖的延时时间可以通过自定义指令的参数传过来，如`v-resize:10`表示10ms延时
                // 也可以将此处延时去掉，放在绑定的函数中自定义
                function debounce(fn, delay = 1) {
                    let t = null
                    return function () {
                        if (t) {
                            clearTimeout(t)
                        }
                        const context = this
                        const args = arguments
                        t = setTimeout(function () {
                            fn.apply(context, args)
                        }, delay)
                    }
                }

                el._resizer = new window.ResizeObserver(debounce(binding.value, Number(binding.arg) || 1))
                el._resizer.observe(el)
            },
            unmounted(el) {
                el._resizer.disconnect()
            },
        });
    }
};

// 文字复制剪贴
import Clipboard from 'clipboard'
// v-clipboard 文字复制剪贴
export const clipboard = {
    install(Vue: any, options: any) {
        Vue.directive('clipboard', {
            bind(el, binding, vnode) {
                switch (binding.arg) {
                    case 'success':
                        el._vClipBoard_success = binding.value;
                        break;
                    case 'error':
                        el._vClipBoard_error = binding.value;
                        break;
                    default: {
                        const clipboard = new Clipboard(el, {
                            text: () => binding.value,
                            action: () => binding.arg === 'cut' ? 'cut' : 'copy'
                        });
                        clipboard.on('success', e => {
                            const callback = el._vClipBoard_success;
                            callback && callback(e);
                        });
                        clipboard.on('error', e => {
                            const callback = el._vClipBoard_error;
                            callback && callback(e);
                        });
                        el._vClipBoard = clipboard;
                    }
                }
            },
            update(el, binding) {
                if (binding.arg === 'success') {
                    el._vClipBoard_success = binding.value;
                } else if (binding.arg === 'error') {
                    el._vClipBoard_error = binding.value;
                } else {
                    el._vClipBoard.text = function () {
                        return binding.value;
                    };
                    el._vClipBoard.action = () => binding.arg === 'cut' ? 'cut' : 'copy';
                }
            },
            unbind(el, binding) {
                if (!el._vClipboard) return
                if (binding.arg === 'success') {
                    delete el._vClipBoard_success;
                } else if (binding.arg === 'error') {
                    delete el._vClipBoard_error;
                } else {
                    el._vClipBoard.destroy();
                    delete el._vClipBoard;
                }
            }
        });
    }


}




